﻿using System.Text.Json;
using IS_Ever_Green.DataAccess.Model;
using Microsoft.EntityFrameworkCore;

namespace IS_Ever_Green.DataAccess
{
    public class Seed
    {
        public void ImportDataFromJson()
        {
            using (var context = new EverGreenDBContext())
            {

                var importStatus = context.ImportStatus.FirstOrDefault();

                if (importStatus != null && importStatus.IsDataImported)
                {
                    return;
                }

                string jsonFilePath = @"../../../../IS_Ever_Green.DataAccess/Seed.json";

                var jsonFile = Path.GetFullPath(jsonFilePath);

                if (!File.Exists(jsonFile))
                {
                    return;
                }

                string json = File.ReadAllText(jsonFile);

                var data = JsonSerializer.Deserialize<DataModel>(json);



                context.Categories.AddRange(data.Categories);
                context.Manufacturers.AddRange(data.Manufacturers);
                context.Roles.AddRange(data.Roles);
                context.Products.AddRange(data.Products);


                context.Users.AddRange(data.Users);

                context.SaveChanges();

                context.Orders.AddRange(data.Orders);
                context.Reviews.AddRange(data.Reviews);

                context.SaveChanges();

                context.OrderItems.AddRange(data.OrderItems);
                context.Carts.AddRange(data.Carts);

                foreach (var user in data.Users)
                {
                    var selectedFileName = user.PhotoPath;

                    if (!string.IsNullOrEmpty(selectedFileName))
                    {
                        byte[] photoBytes = File.ReadAllBytes(selectedFileName);

                        user.PhotoUser = photoBytes;
                    }
                }


                foreach (var product in data.Products)
                {
                    string selectedFileName;

                    if (product.PhotoPath != null)
                    {
                        selectedFileName = product.PhotoPath;
                    }
                    else
                    {
                        selectedFileName = "../../../../IS_Ever_Green/Images/Default_product_image.png";
                    }
                   

                    if (!string.IsNullOrEmpty(selectedFileName))
                    {
                        byte[] photoBytes = File.ReadAllBytes(selectedFileName);

                        product.PhotoProduct = photoBytes;
                    }
                }


                if (importStatus == null)
                {
                    importStatus = new ImportStatus { IsDataImported = true };
                    context.ImportStatus.Add(importStatus);
                }
                else
                {
                    importStatus.IsDataImported = true;
                    context.Entry(importStatus).State = EntityState.Modified;
                }

                context.SaveChanges();
            }
        }
    }

    public class DataModel
    {
        public List<CartModel> Carts { get; set; }
        public List<CategoryModel> Categories { get; set; }
        public List<ManufacturerModel> Manufacturers { get; set; }
        public List<OrderItemModel> OrderItems { get; set; }
        public List<OrderModel> Orders { get; set; }
        public List<ProductModel> Products { get; set; }
        public List<ReviewModel> Reviews { get; set; }
        public List<RoleModel> Roles { get; set; }
        public List<UserModel> Users { get; set; }
    }
}
